package com.yk._06_Concurrent的三个工具类;

import java.util.concurrent.Semaphore;

public class _03Semaphore {


    /**
     * 模拟10辆车,三个停车位
     * @param args
     */
    public static void main(String[] args) throws InterruptedException {
        Semaphore semaphore = new Semaphore(3);
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                try {
                    System.out.println(Thread.currentThread().getName()+" 正在停车位");
                    semaphore.acquire();
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    System.out.println(Thread.currentThread().getName()+" 离开车位");
                    semaphore.release();
                }
            }).start();

        }
    }
}
